package test.wk.job.heartbeat.master;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

import com.wk.job.Job;
import com.wk.job.Task;
import com.wk.job.heartbeat.master.JobStateHeartbeatHandler;
import com.wk.net.heartbeat.handler.receivers.TcpReceiver;


public class DemoJobStateHeartbeatHandler {
	public static void main(String[] args) {
		final int CHECK_PERIOD = 10;// 检查心跳的周期（单位：秒）
		final int ABSENCE_TOLERANCE = 3;// 掉线容忍度

		final int DUMP_PERIOD = 5;// 显示所有Job的状态的周期（单位：秒）

		ConcurrentHashMap<String, Job> jobs = new ConcurrentHashMap<>();
		ConcurrentHashMap<Integer,Task> tasks = new ConcurrentHashMap<>();
		
		jobs.put("2017-4-611", new Job("降雨侵蚀力", "11", 50, "2017-4-6"));
		jobs.put("2017-4-753", new Job("降雨侵蚀力", "53", 0, "2017-4-7"));

		TcpReceiver receiver = new TcpReceiver(8086);
		try {
			receiver.listen();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		JobStateHeartbeatHandler handler = new JobStateHeartbeatHandler(receiver, jobs,tasks, CHECK_PERIOD,
				ABSENCE_TOLERANCE);
		/* TODO 从数据库中读取计算资源的IP地址 */
		handler.observe("127.0.0.1", true);
		handler.observe("123.57.22.224", true);

		handler.start();

		while (true) {
			System.out.println("显示所有Job……");
			for (Job j : jobs.values()) {
				System.out.println("[job]" + j.toString());
			}
			try {
				Thread.sleep(DUMP_PERIOD * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
				break;
			}
		}
	}
}
